<template>
<el-container>
  <el-header style="background-color: white; color: black;">
      <h1 align="center">库存操作</h1>
  </el-header>
  <el-main>
    <!-- 查询条件表单 -->
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="商品名字">
        <el-input v-model="queryForm.goodsName" placeholder="商品名字"></el-input>
      </el-form-item>
      <el-form-item label="商品类型">
        <el-input v-model="queryForm.goodsType" placeholder="商品类型"></el-input>
      </el-form-item>
      <el-form-item label="仓库Id">
        <el-input v-model="queryForm.storageId" placeholder="仓库Id"></el-input>
      </el-form-item>
      <el-form-item label="供货人">
        <el-input v-model="queryForm.goodsSuppliersName" placeholder="供货人"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
        <el-button type="primary" @click="addInventory()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="100%" >
      <!-- 列组件 -->
      <!-- prop 对应当前页集合的对象属性-->
      <el-table-column prop="goodsId" label="编号" width="50"></el-table-column>
      <el-table-column prop="goodsName" label="商品名字"style="0%"></el-table-column>
      <el-table-column prop="goodsType" label="商品类型"  width="100"></el-table-column>
      <el-table-column prop="goodsPrice" label="商品价格" width="80"></el-table-column>
      <el-table-column prop="goodsNums" label="商品数量" width="80"></el-table-column>
      <el-table-column prop="goodsJoinTime" label="商品加入时间" style="0%"></el-table-column>
      <el-table-column prop="storageId" label="仓库Id" width="80" :formatter="storageId"></el-table-column>
      <el-table-column prop="goodsUserName" label="入库人" width="80"></el-table-column>
      <el-table-column prop="goodsSuppliersName" label="供货人"width="80"></el-table-column>
      <el-table-column label="操作" style="0%">
          <!-- slot-scope 插槽: 用来获取循环的迭代变量-->
          <template slot-scope="scope" >
            <el-button type="danger" size="mini" @click="joinStorage(scope.row,scope.$index)">修改</el-button>
            <el-button type="danger" size="mini" @click="outStorage(scope.row,scope.$index)">出库</el-button>
          </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
    @size-change="select"
    @current-change="selectPage"
    :current-page="queryForm.page"
    :page-sizes="[10,15,20]"
    :page-size="queryForm.row"
    :total="total"
    layout="total,sizes,prev,pager,next,jumper"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="isShow" width="50%">
    <el-form :model="storage" status-icon  ref="storage" label-width="100px" class="demo-ruleForm">
      <el-form-item label="商品名字" prop="goodsName">
          <el-input v-model="storage.goodsName" ></el-input>
      </el-form-item>
      <el-form-item label="商品类型" v-show="storage.isShow" prop="userName">
          <el-input v-model="storage.goodsType" @input="search"/>
      </el-form-item>
      <el-form-item label="商品价格" prop="goodsPrice">
          <el-input-number v-model="storage.goodsPrice" :min="1"></el-input-number>
      </el-form-item>
      <el-form-item label="商品数量" prop="goodsNums">
          <el-input-number v-model="storage.goodsNums" :min="1"></el-input-number>
      </el-form-item>
      <el-form-item label="商品加入时间" prop="goodsJoinTime" v-show="!storage.isShow">
        <div class="block">
            <el-date-picker
              v-model="storage.goodsJoinTime"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
      </el-form-item>
      <el-form-item label="供货人" prop="goodsSuppliersName">
          <el-input v-model="storage.goodsSuppliersName" ></el-input>
      </el-form-item>
        <el-select v-model="storage.storageId" placeholder="请选择仓库">
            <el-option
              v-for="(item,index) in options"
              :key="index"
              :label="item.storageId"
              :value="item.storageId">
            </el-option>
        </el-select>
        <el-button @click="submitForm(index)">提交</el-button>
    </el-form>
    </el-dialog>
  </el-main>
</el-container>
</template>

<script>
  export default {
      data() {
        return {
          tableData: [],
          totalPage: 0, //总页数
          total: 0, //总条数
          title:"",
          isShow:false,
          index:0, //0为修改  1为添加
          queryForm: {
            page: 1,
            row: 10,
            goodsName:"",
            goodsType:"",
            storageId:"",
            goodsSuppliersName:"",
          },
          storage:{
            storageId:"",
            goodsId:"",
            isShow:false,
            goodsName:"",
            goodsType:"",
            goodsPrice:"",
            goodsNums:"",
            goodsJoinTime:"",
            goodsUserName:"",
            goodsSuppliersName:""
          },
          options:[],
        }
      },
      methods: {
        loadData() { //加载数据
          var self = this;
          var http = this.$http.post("/api/goods/inventoryOperation",this.$qs.stringify(this.queryForm));
          http.then(function(rs) {
            if (rs.data.code == 200) {
              self.tableData = rs.data.data.data;
              self.total = rs.data.data.total;
              self.totalPage = rs.data.data.totalPage;
            }else{
              self.tableData=[];
              self.total=0;
            }
          })
        },
        select(row){ //分页下拉框选中触发的事件，row是当前选中条数
          this.queryForm.row=row;
          //刷新
          this.loadData();
        },
        selectPage(page){//选中页码触发的事件, page就是选中的页码
          this.queryForm.page = page;
          //刷新
          this.loadData();
        },
        joinStorage(x,index) { //入库
          this.index = 0
          this.storage = x;
          this.title = "修改入库";
          this.isShow = true;
          var self = this;
          var http = this.$http.post("api/goods/selectStorageByType",this.$qs.stringify(x));
          http.then(function(rs){
              if(rs.data.code == 200){
                self.options = rs.data.data;
              }
          })
        },
        outStorage(x,index) { //出库
          this.storage.goodsId = x.goodsId;
          var self = this
          var http = this.$http.post("api/goods/outStorage",this.$qs.stringify(this.storage));
          http.then(function(rs){
                alert(rs.data.msg)
                location.reload();
          })
        },
        storageId(row){
           if(row.storageId==null){
            return "未入库";
           }else{
             return row.storageId;
           }
        },
        submitForm(i){
          if(i == 0){
            var self = this
            var http = this.$http.post("api/goods/joinStorage",this.$qs.stringify(this.storage));
            http.then(function(rs){
                  alert(rs.data.msg)
                  location.reload();
            })
          }else{
            var self = this
            var http = this.$http.post("api/goods/addStorage",this.$qs.stringify(this.storage));
            http.then(function(rs){
                  alert(rs.data.msg)
                  location.reload();
            })
          }

        },
        addInventory(){
          this.options = [];
          this.index = 1;
          this.storage = {};
          this.title = "添加";
          this.isShow = true;
          this.storage.isShow = true;
          this.storage.goodsUserName = sessionStorage.getItem("name")
        },
        search(){
          var self = this
          var http = this.$http.post("api/goods/selectStorageByType",this.$qs.stringify(this.storage));
          http.then(function(rs){
              if(rs.data.code == 200){
                self.options = rs.data.data;
              }
          })
        }
      },
      mounted(){
        this.loadData();
      }
    }
</script>

<style>
  .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }
</style>
